package com.example.sample.activity;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.SampleApp;

import net.liteheaven.xblib.common.log.XLog;
import net.liteheaven.xblib.common.ui.ToastUtil;

public class AttachBaseContextActivity extends BaseActivity {

    private View showToastButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attach_base_context);
        showToastButton = findViewById(R.id.showToastButton);
        showToastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show(AttachBaseContextActivity.this, "这是一个Toast");
            }
        });
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        //super.attachBaseContext(new CustomContext(newBase));
        super.attachBaseContext(newBase);
    }

    public static class CustomContext extends ContextWrapper{
        private static final String TAG = "CustomContext";
        public CustomContext(Context context){
            super(context);
        }
        @Override
        public Object getSystemService(String name) {
            XLog.i(TAG, "getSystemService " + name);
            if (name != null && name.equals(Context.WINDOW_SERVICE)){
                final WindowManager windowManager = (WindowManager)super.getSystemService(name);
                WindowManager newWindowManager = new WindowManager() {
                    @Override
                    public Display getDefaultDisplay() {
                        return windowManager.getDefaultDisplay();
                    }

                    @Override
                    public void removeViewImmediate(View view) {
                        windowManager.removeViewImmediate(view);
                    }

                    @Override
                    public void addView(View view, ViewGroup.LayoutParams params) {
                        WindowManager.LayoutParams wm = (WindowManager.LayoutParams)params;
                        XLog.i(TAG, "addView type=" + wm.type);
                        if (wm.type == WindowManager.LayoutParams.TYPE_TOAST){
                            Context ctx = SampleApp.getApp().getApplicationContext();
                            Toast.makeText(ctx, "此Activity不允许显示Toast", Toast.LENGTH_SHORT).show();
                        }else{
                            windowManager.addView(view, params);
                        }
                    }

                    @Override
                    public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
                        windowManager.updateViewLayout(view, params);
                    }

                    @Override
                    public void removeView(View view) {
                        windowManager.removeView(view);
                    }
                };
                return newWindowManager;
            }else{
                return super.getSystemService(name);
            }
        }
    }
}
